﻿using Bussiness.Interface;
using Bussiness.Service;
using Common;
using Common.Model;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    public List<Users> user = new List<Users>();
    public IBaseService baseService = new BaseService();
    Users users = new Users();
    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox1.Visible = false;
        TextBox2.Visible = false;
        Label1.Visible = false;
        Label2.Visible = false;
        if (Session["user"] != null)
        {
            users = (Users)Session["user"];
            Result<List<Users>> list = new Result<List<Users>>();
            list = baseService.Select<Users>(p=>p.UserID==users.UserID);
            if (list.Data != null)
            {
                try
                {
                    user = list.Data;
                }
                catch (Exception ex)
                {
                    user = null;
                }
            }
        }
        else
        {
            Response.Redirect("Login.aspx");
        }
        
    }

    protected void changePassword_Click(object sender, EventArgs e)
    {
        if (changePassword.Text == "修改密码")
        {
            TextBox1.Visible = true;
            TextBox2.Visible = true;
            Label1.Visible = true;
            Label2.Visible = true;
            changePassword.Text = "确认修改";
            foreach (var item in user)
            {
                TextBox1.Text = item.Password;
            }
        }
        else if(changePassword.Text=="确认修改")
        {
            if (inputvalue())
            {
                if (baseService.Update(users).Data)
                {
                    Response.Write("<script>alert('修改成功')</script>");
                    TextBox1.Visible = false;
                    TextBox2.Visible = false;
                    Label1.Visible = false;
                    Label2.Visible = false;
                    changePassword.Text = "修改密码";
                }
            }
            else
            {
                changePassword.Text = "修改密码";
            }

        }
        
    }
    public bool inputvalue()
    {
        
        if (TextBox2.Text != "" & TextBox2.Text.Length > 5)
        {
            users.Password = MD5Encrypt.Encrypt(TextBox2.Text);
            return true;
        }
        else
        {
            Response.Write("<script>alert('密码不能为空且密码长度不能小于6位')</script>");
            return false;
        }
    }
}